package com.duolebo.uteped_sdk.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class LocationHelper implements LocationListener {

    private final static String TAG = "LocationHelper";
    private LocationManager locationManager;
    @SuppressLint("StaticFieldLeak")
    private static LocationHelper instance;
    public static LocationHelper getInstance() {
        if (instance == null) instance = new LocationHelper();

        return instance;
    }

    public LocationHelper() {
    }

    public void stopLocation() {
        Logger.d(TAG, "stopLocation");
        if (locationManager != null) {
            locationManager.removeUpdates(this);
            locationManager = null;
        }
    }

    public void startLocation() {
        Logger.d(TAG, "startLocation");
        if (locationManager == null) {
            locationManager = (LocationManager) Tools.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, this);
    }

    @Override
    public void onProviderDisabled(@NonNull String provider) {
        Logger.d(TAG, "onProviderDisabled");
        onSignalLevelChanged(0);
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        Logger.d(TAG, "onLocationChanged: " + location);
        int level = 0;
        Bundle bundle = location.getExtras();
        if (bundle != null) {
//            float snr = bundle.getFloat("snr");
//            int satellites =  (int) (snr * 100);
//            if (satellites < 25) {
//                level = 1;
//            }
//            else if (satellites < 50) {
//                level = 2;
//            }
//            else if (satellites < 75) {
//                level = 3;
//            }
//            else {
//                level = 4;
//            }
            int satellites =  bundle.getInt("satellites");
            if (satellites < 5) {
                level = 1;
            }
            else if (satellites < 10) {
                level = 2;
            }
            else if (satellites < 15) {
                level = 3;
            }
            else {
                level = 4;
            }
        }

        onSignalLevelChanged(level);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        GpsStatus ges = locationManager.getGpsStatus(null);      //获取GPS状态
        switch (status) {
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                break;
            //定期发送事件以报告 GPS 卫星状态。
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                Iterable<GpsSatellite> lis = ges.getSatellites();       //获取卫星信息迭代数组
                Iterator it = lis.iterator();

                while (it.hasNext()) {
                    GpsSatellite s = (GpsSatellite) it.next();
                    float id = s.getSnr();     //卫星id
                    float DBHz = s.getSnr();     //卫星信号强度
                    float azimuthDegrees = s.getAzimuth();  //卫星方位角
                    boolean infix = s.usedInFix();  //卫星是否可用于定位
                    float elevationDegrees = s.getElevation();     //卫星高程
                    boolean almanacData =s.hasAlmanac();   //卫星是否具有年历数据
                    boolean ephemerisData = s.hasEphemeris();      //卫星是否具有星历数据
                }
                break;
            case GpsStatus.GPS_EVENT_STARTED:
                break;
            case GpsStatus.GPS_EVENT_STOPPED:
                break;
            default:
                break;
        }
    }

    private void onSignalLevelChanged(int level) {
        Logger.d(TAG, "onSignalLevelChanged:" + level);
        if (this.locationListener != null) {
            this.locationListener.onSignalChanged(level);
        }
    }

    /**
     * setLocationChangeListener
     * @param listener listener
     */
    public void setLocationChangeListener(LocationChangeListener listener) {
        this.locationListener = listener;
    }
    private LocationChangeListener locationListener;
    public interface LocationChangeListener {
        /**
         *
         * @param signal 强度0-4档
         */
        void onSignalChanged(int signal);
    }

}
